package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.model.PositionEntity;
import com.gears_software.sxdc.dccx.ui.map.RouteTask;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/9/1.
 */

@EActivity(R.layout.activity_yuyue)
public class YuYueActivity extends AppCompatActivity implements RouteTask.OnRouteCalculateListener {

    @App
    DCCXApplication app;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView right_icon;

    @ViewById
    TextView txt_chengcheren;
    @ViewById
    TextView txt_shang_time;
    @ViewById
    TextView txt_shang_didian;
    @ViewById
    TextView txt_xia_didian;
    @ViewById
    TextView txt_shangche_address;
    @ViewById
    TextView txt_xiache_address;
    @ViewById
    TextView txt_chufa_time;
    @ViewById
    TextView txt_yugu;
    @ViewById
    Button btn_callcar;
    @ViewById
    RelativeLayout re_start_time;
    @ViewById
    RelativeLayout re_start;
    @ViewById
    RelativeLayout re_end;
    @Extra
    PositionEntity startEntity;
    private boolean[] type = new boolean[]{false, true, true, true, true, false};//显示类型 默认全部显示
    TimePickerView pvTime;
    RouteTask routeTask;

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        initTimePick();
        routeTask = RouteTask.getInstance(getApplicationContext());
        routeTask.addRouteCalculateListener(this);
        if (startEntity != null) {
            routeTask.setStartPoint(startEntity);
//            RouteTaskNew.getInstance(getApplicationContext()).search();
            if (startEntity.getName() != null && !startEntity.getName().equals("")) {
                txt_shangche_address.setText(startEntity.getName());

            } else {
                String address = startEntity.getAddress().replace(startEntity.getProvince(), "").replace(startEntity.getCity(), "").replace(startEntity.getDistrict(), "").replace(startEntity.getStreet(), "");
                txt_shangche_address.setText(address);
            }
        }
    }

    void initIconFont() {
        actionbar_title.setText("预约用车");
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        txt_chengcheren.setTypeface(app.iconfont);
        txt_shang_time.setTypeface(app.iconfont);
        txt_shang_didian.setTypeface(app.iconfont);
        txt_xia_didian.setTypeface(app.iconfont);
    }

    void initTimePick() {
        Calendar startDate = Calendar.getInstance();
        startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.set(endDate.get(Calendar.YEAR), endDate.get(Calendar.MONTH), endDate.get(Calendar.DAY_OF_MONTH) + 3);
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                txt_chufa_time.setText(getTime(date));
            }
        })
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.GRAY)//取消按钮文字颜色
                .isCyclic(false)//是否循环滚动
                .setType(type)
                .setDate(Calendar.getInstance())// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .setLabel("年", "月", "日", "", "", "")
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    @Click
    void re_start_time() {
        pvTime.show();
    }

    @Click
    void re_start() {
        Intent intent = new Intent(this, DestinationActivity_.class);
        intent.putExtra("title", "选择上车地点");
        intent.putExtra("flag", "start");
        intent.putExtra("currentCity", startEntity.getCity());
        startActivityForResult(intent, 1);
    }

    @Click
    void re_end() {
        Intent intent = new Intent(this, DestinationActivity_.class);
        intent.putExtra("title", "选择下车地点");
        intent.putExtra("flag", "end");
        intent.putExtra("currentCity", startEntity.getCity());
        startActivityForResult(intent, 2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            PositionEntity entity = (PositionEntity) data.getExtras().getSerializable("entity");
            if (requestCode == 1) {
                txt_shangche_address.setText(entity.getName());
            } else if (requestCode == 2) {
                txt_xiache_address.setText(entity.getName());
            }
        }
    }

    @Click
    void back_icon() {
        this.finish();
    }

    @Override
    public void onRouteCalculate(float cost, float distance, int duration) {
        btn_callcar.setBackgroundResource(R.color.colorActionbar);
        btn_callcar.setEnabled(true);
        txt_yugu.setText(String.format("%.2f元，距离%.1fkm，用时%d分", cost,
                distance, duration));
    }

    @Click
    void btn_callcar() {
        if (txt_chufa_time.getText().toString().equals("")) {
            TastyToast.makeText(this, "请选择出发时间", app.SHORT, app.WARNING);
        } else if (txt_shangche_address.getText().toString().equals("")) {
            TastyToast.makeText(this, "请选择上车地点", app.SHORT, app.WARNING);
        } else if (txt_xiache_address.getText().toString().equals("")) {
            TastyToast.makeText(this, "请选择下车地点", app.SHORT, app.WARNING);
        } else {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        routeTask.clearEndPoint();
        routeTask.removeRouteCalculateListener(this);
    }
}
